Thread: Encrypt & Decrypt Input String [Help Please]

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    4

    Encrypt & Decrypt Input String [Help Please]

    Hi guys, I'm new here and also new to C programming. So i got an assignment on C about encrypt and decrypt the string input. I need to use all what I've learn so far which the basic, array, function, pointer, string. I've already learn file processing but I'm not sure if I can include it in this assignment.


    Basically, user need to input a string so that the program will decrypt it into other character(in my case I'm encrypting the string into alphanumeric char so that it will be easy to read by user.). Then the program will decrypt the encrypted string back to it's origin.


    Here's how the flow should works :


    Input (user input string) => gh6T0 (encrypted string)=> Input (decrypt to original string)


    Here I've write some code but it's a little bit messy but hope you guys can read it. The problem I faced here's that I can't decrypt the value back to origin since I'm using modulo in the encrypted part (modulo is irreversible calculation), so that means impossible for me to decrypt back to origin string. Anyone please help me overcome this please :'( Thanks in advance, and pardon for my bad english.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    #define SIZE 10 //string max size
    
    
    void encrypt(char [], int []);    //function prototype for encrypt
    void decrypt(char [], int []);    //function prototype for decrypt
    
    
    int main()    //main function/body
    {
        char password[SIZE];    //input array
        int randNO[SIZE];        //random number array to be used in both enc&decrypt function
        int j=0, count;
        srand(time(NULL));
    
    
        puts("Enter your password: ");
        gets(password);        //get input string from user
        
        count=strlen(password);
         
         for(j=0;j<count;j++)
         randNO[j]=10+rand()%(10000-10+1);    //generate random number to be used in enc&decrypt function
        
        encrypt(password, randNO);        //encrypt function call
    
    
        decrypt(password, randNO);        //decrypt function call
        
        system("pause");
        return 0;
    }
    
    
    void encrypt(char *PASSWORD, int *randNO)        //encrypt function body
    {
         int i=0, j=0, k=0;
         int count=0;
         char alphanum[] ="0123456789""abcdefghijklmnopqrstuvwxyz""ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         
         count=strlen(PASSWORD);
         
         for(j=0;j<count;j++)
         randNO[j]=10+rand()%(10000-10+1);
    
    
         printf("Your password is : ");
         puts(PASSWORD);
         printf("Encryption in progress...\n");
         
         for(i=0;i<count;i++)
         {
            PASSWORD[i] = alphanum[randNO[i]%(sizeof(alphanum) - 1)];    //encrypting
            
          
         }
         PASSWORD[count]='\0';
         puts(PASSWORD);        //encrypted value will be printed here
         
         return ;
    }
    
    
    void decrypt(char *PASSWORD, int *randNO)        //decrypt function body
    {
         int i=0, j=0, k=0;
         int count=0;
         char alphanum[] ="0123456789""abcdefghijklmnopqrstuvwxyz""ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         
         count=strlen(PASSWORD);
         printf("Your encrypted password is : ");
         puts(PASSWORD);
         
         printf("Decryption in progress...\n");
         
         for(i=0;i<count;i++)
         {
            PASSWORD[i] = alphanum[randNO[i]%(sizeof(alphanum) + 1)];
            
          
         }
         PASSWORD[count]='\0';
         puts(PASSWORD);
         
         return ;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If the encryption is irreversible, then it was wrong to use it in the first place. In this case, the irreversibility comes not from the modulo operator, but from the fact that you can't retrieve those random numbers again.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This isn't encryption, it's a bunch of random letters!
    You need to go back to the drawing board and think about what you're trying to do.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    @tabstop, that's why im putting the random number array in the main function. Once the program generate the rand number and save it in array, i can call it in both in the encrypt and decrypt . Which means the rand only generated once, and can be retrieve in decrypt function. So that means the modulo part become the problem here. If only i can generate random without using modulo (other equation to get the rand), then it'll be great. By the way, do you know how i could change it according to your idea which is not to use the rand

    and @oogabooga, hmm, pardon me man, i really thought by generating random character like this is part of encryption. I found one example about encrypt and decrypt on C Program Examples: C program to Encrypt and Decrypt a password but the encryption generated was some unreadable character by human. Here's the example they put on the website

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void encrypt(char password[],int key)
    {
        unsigned int i;
        for(i=0;i<strlen(password);++i)
        {
            password[i] = password[i] - key;
        }
    }
    
    void decrypt(char password[],int key)
    {
        unsigned int i;
        for(i=0;i<strlen(password);++i)
        {
            password[i] = password[i] + key;
        }
    }
    int main()
    {
        char password[20] ;
        printf("Enter the password: \n ");
        scanf("%s",password);
        printf("Passwrod     = %s\n",password);
        encrypt(password,0xFACA);
        printf("Encrypted value = %s\n",password);
        decrypt(password,0xFACA);
        printf("Decrypted value = %s\n",password);
        return 0;
    }
    


    About the 0xFACA he use as the key, is it some hexadecimal? He stated in the comment there that it can be change with other value, but how can I change it? Btw thanks to you guys for responding to my thread. I really appreaciate that

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you actually think that that array of random numbers you create in main is getting used anywhere at all? If so, we are going to have to back up quite a ways. (EDIT: Let me back up a bit; you do overwrite that array in your encrypt function, but thankfully your decrypt function doesn't overwrite it also, so assuming you only call encrypt once (which in real life wouldn't happen) and call decrypt in the same program you call encrypt (this will certainly never happen, since the whole point of decryption is to do it later) then you can reuse the numbers.)

    The irreversibility has nothing to do with % and everything to do with the fact that you are not even using the original letter at all. (The problem you may sort-of have with % would come from ASCII not having digits and characters consecutive. If you used characters from (say) 33-122 as both input and output and used mod 90, there would be no difficulty at all reconstructing the original. (There would be many solutions to the equation you would have to solve, but by definition they will all be 90 apart and therefore only one of them will be in the range from 33-122.)
    Last edited by tabstop; 12-02-2013 at 11:25 AM.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    Ahh, I see. So i need to do at least 2 or more level of encryption right, and here comes the file i/o part where I save the encrypted code, then later I will make new program and open the encrypted code back and decrypt it to the original string . Ahhh, so that's the real meaning of encrypt/decrypt thingy, now I understand what oogabooga trying to say. Pardon me for my misunderstanding.

    Oops, I just realized that i didnt even use the original string the user input in the first place. Thank you so much for enlighten me up. I'll try work out something on that one like you mentioned. Thanks.

    And by the way, can you explain a little the example i get from the other website that I pasted above. The key value which is 0xFACA really bother me somehow.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Using 0xFACA is a bit weird, as it is a 16-bit number and therefore larger than a char. The operations happen as an int (either 16- or 32-bit depending on the hardware I suppose), but then when it gets stored back as a character only the last eight bits are kept. At the moment I can't see any difference with just using 0xCA instead (I don't think there's any difference that will happen with signed/unsigned char, but I might have missed something). If someone managed to get 0xCA (which for me on this Windows system is ╩) into the string (by hitting Alt-202 or perhaps getting UTF8 into the input, although that's in a weird section of the Unicode table) then the program will give erroneous results (as that will encrypt to a NUL character and therefore prematurely end the string).

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    I see, Thanks for the explaination. Now I need to work out something on my code. Thank you

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Code:
    For permissions to use the programs for commercial purposes, contact ....
    That's pretty funny.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encrypt/Decrypt text
    By patneel in forum C Programming
    Replies: 6
    Last Post: 09-06-2011, 02:55 PM
  2. [Help Me]Very Simply Encrypt/Decrypt
    By ShiroAzure in forum C++ Programming
    Replies: 11
    Last Post: 04-18-2011, 12:58 PM
  3. decrypt/encrypt
    By wonderpoop in forum C Programming
    Replies: 15
    Last Post: 10-18-2006, 06:10 PM
  4. Encrypt/Decrypt
    By bitWise in forum C Programming
    Replies: 2
    Last Post: 10-14-2001, 03:48 PM
  5. how to encrypt/decrypt
    By bitWise in forum C Programming
    Replies: 3
    Last Post: 10-13-2001, 01:02 PM

Tags for this Thread